package inl.resilienceManager.qmul.gemom;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * The class used for the variance table of namespace
 * 
 * The constant number of namespace is used
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */

public class varTable {

	JTable table;
	DefaultTableModel model;

	private static int numV= 0;

	JScrollPane scrollPane;

	JPanel varPane;

	JButton jButton_modify;


	public varTable(Object[][] data){
		numV=data.length;
		String[] columnNames= new String[numV];
		HashMap<String,String> idns = TableSaver.getMapIDNS();
		for(int i=1; i<numV+1; i++)
		{
//			columnNames[i-1]="N"+i;
			columnNames[i-1]=idns.get(""+i);
		}

		
		model= new DefaultTableModel(data, columnNames);


		table = new JTable(model){
		      public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int vColIndex) {
		    	  Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
		            if (c instanceof JComponent) {
		                JComponent jc = (JComponent)c;
		                jc.setToolTipText(getValueAt(rowIndex, vColIndex).toString());
		            }
		            return c;
		      }
		     };


		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn column = null;
		
		for (int i = 0; i < numV; i++) {
		    column = table.getColumnModel().getColumn(i);
		    column.setPreferredWidth(80);
		}


		

	}

	public JPanel getTable()
	{
		varPane= new JPanel();

		JPanel ButtonPane = new JPanel();
		
		ButtonPane.add(getJButton_modify());

		scrollPane= new JScrollPane(table);
		
		varPane.setLayout( new BorderLayout() );

		varPane.add(scrollPane,BorderLayout.CENTER);
		
		varPane.add(ButtonPane,BorderLayout.SOUTH);


		return varPane;
	}


	/**
	 * This method initializes jButton_modify	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton_modify() {
		if (jButton_modify == null) {
			jButton_modify = new JButton();
			jButton_modify.setText("Save");
			jButton_modify.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					SaveAction();


				}
			});
		}
		return jButton_modify;
	}

// TODO:  
	private void SaveAction(){
		
		
		float[][] data= new float[numV][numV];
		
		for(int i=0; i<numV; i++)
		{
			for(int j=0; j<numV; j++)
			
			{
				data[i][j]= Float.parseFloat((model.getValueAt(i, j)).toString());


			}
		}
		
		TableSaver.Saving(data);
	}

}
